package mess;

import javax.swing.tree.TreeNode;

public class LeeCode_110 {
    public static boolean res = true;

    public static void main(String[] args) {
        System.out.println(isBalanced(null));
    }
    private static boolean isBalanced(TreeNode root) {
        helper(root);
        return res;
    }

    private static int helper(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int l = helper(node.left) + 1;
        int r = helper(node.right) + 1;
        if (Math.abs(r - l) > 1) {
            res = false;
            return Math.max(l, r);
        }
        return Math.max(l, r);
    }

    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
